<html>
<script src="jquery-1.6.1.min.js"></script>
<script>
	//var REST_HOST = "http://127.0.0.1:5000";
	var REST_HOST = "http://www.msg2phone.dotcloud.com";

	function onClickHandler(info, tab) {
		console.log("clicked");
		var url = info.srcUrl;
		if (url == undefined) url = info.linkUrl;
		if (url == undefined) url = tab.url;
		
		var msgType = info.mediaType;
		if (msgType == undefined) msgType = 'page';
		
		var sel = info.selectionText;
		if (sel == undefined) sel = '';
		
		//var bg = chrome.extension.getBackgroundPage();
		/*bg.sendToPhone(tab.title, url, msgType, sel, function(status) {
		  if (status == STATUS_LOGIN_REQUIRED) {
		      // user will have to click the link again
		      // TODO: encode the parameters, re-do the post after login
		      // or TODO: display the 'login required' message first, if regToken is null
		    chrome.tabs.create({url: signInUrl});
		  } 
		});*/
		
		$.getJSON(REST_HOST + '/selection/add', {
	        name: 'zjh',
	        text: sel,
	        type: msgType,
	        url : url
	    }, function(data) {
	    	alert(data.result);
	    });
	}
	
	if (chrome.contextMenus) {
		chrome.contextMenus.create({'title': 'sync to phone',
	                              'documentUrlPatterns': [ 'http://*/*', 'https://*/*' ],
	                              'onclick': onClickHandler,
	                              'contexts': ['link', 'selection', 'image', 'video', 'audio']});
	}

</script>

</html>
